package top.zhangjianyong.blog.util;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Builder;
import lombok.Data;
import top.zhangjianyong.blog.enums.StsOperationType;
import java.util.Collections;
import java.util.List;

/**
 * STS 策略构建器
 * 
 * @author zhangjianyong
 * @since 2024-01-01
 */
public class StsPolicyBuilder {
    
    private static final ObjectMapper objectMapper = new ObjectMapper();
    
    @Data
    @Builder
    public static class PolicyStatement {
        @JsonProperty("Effect")
        private String Effect;
        @JsonProperty("Action")
        private String[] Action;
        @JsonProperty("Resource")
        private String[] Resource;
    }
    
    @Data
    @Builder
    public static class Policy {
        @JsonProperty("Version")
        private String Version;
        @JsonProperty("Statement")
        private List<PolicyStatement> Statement;
    }
    
    /**
     * 构建STS权限策略
     * 
     * @param operationType 操作类型
     * @param bucketName 存储桶名称
     * @param pathPrefix 路径前缀
     * @return JSON格式的策略字符串
     */
    public static String buildPolicy(StsOperationType operationType, String bucketName, String pathPrefix) {
        try {
            // 构建资源路径 - 确保路径格式正确
            String resourcePath = String.format("acs:oss:*:*:%s/%s*", bucketName, pathPrefix);
            
            // 构建策略声明
            PolicyStatement statement = PolicyStatement.builder()
                    .Effect("Allow")
                    .Action(operationType.getActions())
                    .Resource(new String[]{resourcePath})
                    .build();
            
            // 构建完整策略
            Policy policy = Policy.builder()
                    .Version("1")
                .Statement(Collections.singletonList(statement))
                    .build();

            String policyJson = objectMapper.writeValueAsString(policy);
            System.out.println("生成的策略JSON: " + policyJson); // 调试输出

            return policyJson;
            
        } catch (Exception e) {
            throw new RuntimeException("构建STS策略失败", e);
        }
    }
    
    /**
     * 构建自定义路径的STS权限策略
     * 
     * @param operationType 操作类型
     * @param bucketName 存储桶名称
     * @param customPath 自定义路径
     * @return JSON格式的策略字符串
     */
    public static String buildCustomPolicy(StsOperationType operationType, String bucketName, String customPath) {
        try {
            // 构建资源路径 - 确保路径格式正确
            String resourcePath = String.format("acs:oss:*:*:%s/%s*", bucketName, customPath);
            
            // 构建策略声明
            PolicyStatement statement = PolicyStatement.builder()
                    .Effect("Allow")
                    .Action(operationType.getActions())
                    .Resource(new String[]{resourcePath})
                    .build();
            
            // 构建完整策略
            Policy policy = Policy.builder()
                    .Version("1")
                .Statement(Collections.singletonList(statement))
                    .build();

            String policyJson = objectMapper.writeValueAsString(policy);
            System.out.println("生成的自定义策略JSON: " + policyJson); // 调试输出

            return policyJson;
            
        } catch (Exception e) {
            throw new RuntimeException("构建STS策略失败", e);
        }
    }
} 